﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Windows;
using Xpom_Xpum__Integrated_Development_Environment.Model.Controls;

namespace Xpom_Xpum__Integrated_Development_Environment.ViewModel.Controls
{
    public class ProjectExplorerViewModel : ViewModel
    {
        #region Constructor

        public ProjectExplorerViewModel(string projectDirectory)
        {
            Items = new List<ProjectItemModel>();
            var p = new ProjectItemModel(projectDirectory, true);
            Items.Add(p);
            p.Items.AddRange(ProzDir(projectDirectory).ToArray());

            ProjectDirectory = projectDirectory;

            ((App)App.Current).pev.DataContext = this;

            ((App)App.Current).mvm.RecentFiles.Clear();
            ((App)App.Current).mvm.RecentFiles.Add("1");
            ((App)App.Current).mvm.IsProjectOpened = true;
        }

        #endregion

        #region Commands

        #endregion

        #region Fields

        private ProjectItemModel selectedItem;

        public string ProjectDirectory;

        #endregion

        #region Properties

        public List<ProjectItemModel> Items { get; set; }

        public ProjectItemModel SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                selectedItem = value;
            }
        }

        public string ProjectName
        {
            get
            {
                return Path.GetFileName(Path.GetDirectoryName(this.ProjectDirectory + "\\"));
            }
        }

        #endregion

        #region Methods

        private List<ProjectItemModel> ProzDir(string dir)
        {
            var l = new List<ProjectItemModel>();

            var dirz = Directory.GetDirectories(dir);
            foreach (var d in dirz)
            {
                var p = new ProjectItemModel(d + "\\", true);
                l.Add(p);
                p.Items.AddRange(ProzDir(d + "\\").ToArray());
            }

            var filz = Directory.GetFiles(dir);
            foreach (var f in filz)
            {
                var p = new ProjectItemModel(f, false);
                l.Add(p);
            }

            return l;
        }

        #endregion
    }
}
